home *** CD-ROM | disk | FTP | other *** search
/ Almathera Ten Pack 3: CDPD 3 / Almathera Ten on Ten - Disc 3: CDPD3.iso / fish / 726-750 / 741 / rkrm_devices / rkrm_devices.lha / Resources / Query_Serial.c < prev    next >
C/C++ Source or Header  |  1992-09-03  |  4KB  |  137 lines

  1. /*
  2.  * Copyright (c) 1992 Commodore-Amiga, Inc.
  3.  * 
  4.  * This example is provided in electronic form by Commodore-Amiga, Inc. for 
  5.  * use with the "Amiga ROM Kernel Reference Manual: Devices", 3rd Edition, 
  6.  * published by Addison-Wesley (ISBN 0-201-56775-X).
  7.  * 
  8.  * The "Amiga ROM Kernel Reference Manual: Devices" contains additional 
  9.  * information on the correct usage of the techniques and operating system 
  10.  * functions presented in these examples.  The source and executable code 
  11.  * of these examples may only be distributed in free electronic form, via 
  12.  * bulletin board or as part of a fully non-commercial and freely 
  13.  * redistributable diskette.  Both the source and executable code (including 
  14.  * comments) must be included, without modification, in any copy.  This 
  15.  * example may not be published in printed form or distributed with any
  16.  * commercial product.  However, the programming techniques and support
  17.  * routines set forth in these examples may be used in the development
  18.  * of original executable software products for Commodore Amiga computers.
  19.  * 
  20.  * All other rights reserved.
  21.  * 
  22.  * This example is provided "as-is" and is subject to change; no
  23.  * warranties are made.  All use is at your own risk. No liability or
  24.  * responsibility is assumed.
  25.  *
  26.  *****************************************************************************
  27.  *
  28.  *
  29.  * Query_Serial.c
  30.  *
  31.  * We will try to open the serial device and if unsuccessful,
  32.  * will return the name of the owner.
  33.  *
  34.  * Compile with SAS C 5.10  lc -b1 -cfistq -v -y -L
  35.  *
  36.  * Run from CLI only
  37.  */
  38.  
  39. #include <exec/types.h>
  40. #include <exec/memory.h>
  41. #include <dos/dos.h>
  42. #include <resources/misc.h>
  43. #include <devices/serial.h>
  44.  
  45. #include <clib/exec_protos.h>
  46. #include <clib/alib_protos.h>
  47. #include <clib/dos_protos.h>
  48.  
  49. #include <stdio.h>
  50. #include <stdlib.h>
  51.  
  52. #ifdef LATTICE
  53. #include <clib/misc_protos.h>
  54. #include <stdio.h>
  55. int CXBRK(void) { return(0); }  /* Disable SAS CTRL-C handling */
  56. void main(void);
  57. #endif
  58.  
  59. #define UNIT_NUMBER 0
  60.  
  61. struct Library *MiscBase;
  62.  
  63. /* our functions */
  64. void cleanexit(UBYTE *,LONG);
  65. void cleanup(void);
  66.  
  67. struct MsgPort  *SerialMP;         /* Message port pointer */
  68. struct IOExtSer *SerialIO;         /* IORequest pointer */
  69.  
  70. void main()
  71. {
  72. UWORD status;    /* return value of SDCMD_QUERY */
  73. UBYTE *user;     /* name of serial port owner if not us */
  74.  
  75. if (SerialMP=CreatePort(NULL,NULL) )
  76.     {
  77.     if (SerialIO=(struct IOExtSer *)
  78.            CreateExtIO(SerialMP,sizeof(struct IOExtSer)) )
  79.         {
  80.         if (OpenDevice(SERIALNAME,UNIT_NUMBER,(struct IORequest *)SerialIO,0))
  81.             {
  82.             printf("\n%s did not open",SERIALNAME);
  83.  
  84.             MiscBase= (struct Library *)OpenResource(MISCNAME);
  85.  
  86.             /* Find out who has the serial device */
  87.             if ((user = AllocMiscResource(MR_SERIALPORT,"Us")) == NULL)
  88.                 {
  89.                 printf("\n");
  90.                 FreeMiscResource(MR_SERIALPORT);
  91.                 }
  92.             else
  93.                 printf(" because %s owns it \n\n",user);
  94.  
  95.             /* Clean up */
  96.             cleanup();
  97.             }
  98.         else
  99.             {
  100.             SerialIO->IOSer.io_Command  = SDCMD_QUERY;
  101.             SendIO((struct IORequest *)SerialIO);             /* execute query */
  102.  
  103.             status = SerialIO->io_Status;                    /* store returned status */
  104.  
  105.             printf("\tThe serial port status is %x\n",status);
  106.  
  107.             AbortIO((struct IORequest *)SerialIO);
  108.             WaitIO((struct IORequest *)SerialIO);
  109.  
  110.             CloseDevice((struct IORequest *)SerialIO);
  111.             cleanup();
  112.             }
  113.         }
  114.     else
  115.          cleanexit("Can't create IORequest\n",RETURN_FAIL);
  116.     }
  117. else
  118.     cleanexit("Can't create message port\n",RETURN_FAIL);
  119. }
  120.  
  121.  
  122. void cleanexit(UBYTE *s,LONG n)
  123. {
  124. if (*s)
  125.     printf(s);
  126. cleanup();
  127. exit(n);
  128. }
  129.  
  130. void cleanup()
  131. {
  132. if (SerialIO)
  133.     DeleteExtIO(SerialIO);
  134. if (SerialMP)
  135.     DeletePort(SerialMP);
  136. }
  137.